package com.wzg.jetpack_blackbox.room;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.wzg.jetpack_blackbox.login.room.User;
import com.wzg.jetpack_blackbox.login.room.IUserDao;

@Database(
        entities = {//实体表
                User.class
        },
        exportSchema = true,
        version = 1//当前版本号
//        autoMigrations = {//配置自动升级
//                @AutoMigration(from = 1,to = 2,spec = USerSpec.class)
//        }
)
public abstract class AppDataBase extends RoomDatabase {

    private static final String DB_NAME = "blackbox_db";

    public abstract IUserDao getUserDao();

    public static AppDataBase getInstance() {
        return Helper.INSTANCE;
    }

    private final static class Helper {

        private static final AppDataBase INSTANCE = Room.databaseBuilder(Utils.getApp(), AppDataBase.class, DB_NAME)
//                .addMigrations()//迁移
                .allowMainThreadQueries()//禁止主线程操作
                .addCallback(new Callback() {
                    @Override
                    public void onCreate(@NonNull SupportSQLiteDatabase db) {
                        super.onCreate(db);
                        LogUtils.iTag("room", "onCreate***");
                    }

                    @Override
                    public void onOpen(@NonNull SupportSQLiteDatabase db) {
                        super.onOpen(db);
                        LogUtils.iTag("room", "onOpen***");
                    }
                })
                .build();

    }
}
